package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.common.ResultType;
import com.engineering.elink.service.NotifyService;
import com.engineering.elink.util.MapConvert;

@RestController
@RequestMapping("/api/notify")
@Api(value = "通知接口")
public class NotifyController {
	@Autowired
	private NotifyService notifyService;
	
	/**
	 * 我的通知列表
	 * @return
	 */
	@RequestMapping(value="/getList",method = RequestMethod.GET)
	@ApiOperation(value="我的通知列表",httpMethod="GET",notes="我的通知列表")
	public Map<String, Object>getList(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  (String)userInfo.get("userId");
		
		return MapConvert.getMap(ResultType.SUCCESS,notifyService.getList(co,userId));
	}
	/**
	 * 统计未读通知
	 * @return
	 */
	@RequestMapping(value="/getUnreadCount",method = RequestMethod.GET)
	@ApiOperation(value="统计未读通知",httpMethod="GET",notes="统计未读通知")
	public Map<String, Object>getUnreadCount(HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  (String)userInfo.get("userId");
		return MapConvert.getMap(ResultType.SUCCESS,notifyService.getUnreadCount(userId));
	}
	/**
	 * 通知详情
	 * @return
	 */
	@RequestMapping(value="/getInfo",method = RequestMethod.GET)
	@ApiOperation(value="通知详情",httpMethod="GET",notes="通知详情")
	public Map<String, Object>getInfo(@ApiParam(value="通知的主键notifyId",required=true)@RequestParam(value="notifyId",required=true)int notifyId){
		return MapConvert.getMap(ResultType.SUCCESS,notifyService.getInfo(notifyId));
	}
	/**
	 * 删除通知
	 * @return
	 */
	@RequestMapping(value="/del",method = RequestMethod.DELETE)
	@ApiOperation(value="删除通知",httpMethod="GET",notes="删除通知")
	public Map<String, Object>del(@ApiParam(value="通知的主键notifyId集合，用逗号进行分割",required=true)
	@RequestParam(value="notifyId",required=true)String notifyId,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  (String)userInfo.get("userId");
		
		return MapConvert.getMap(ResultType.SUCCESS,notifyService.del(notifyId,userId));
	}
	/**
	 * 清空通知
	 * @return
	 */
	@RequestMapping(value="/clearNotify",method = RequestMethod.DELETE)
	@ApiOperation(value="清空通知",httpMethod="DELETE",notes="清空通知")
	public Map<String, Object>clearNotify(HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  (String)userInfo.get("userId");
		return MapConvert.getMap(ResultType.SUCCESS,notifyService.clearNotify(userId));
	}
}
